$OpenBSD: patch-src_ProcessInfo_cpp,v 1.4 2014/07/10 15:12:59 sthen Exp $
Fix sysctl(3) error handling.
--- src/ProcessInfo.cpp.orig	Tue Jun  3 10:30:56 2014
+++ src/ProcessInfo.cpp	Thu Jul 10 16:12:33 2014
@@ -27,6 +27,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <errno.h>
 #include <unistd.h>
 #include <pwd.h>
 #include <sys/param.h>
@@ -738,38 +739,28 @@ class OpenBSDProcessInfo : public UnixProcessInfo (pub
 
 private:
     virtual bool readProcInfo(int aPid) {
+        struct kinfo_proc   kInfoProc;
         int      managementInfoBase[6];
-        size_t   mibLength;
-        struct kinfo_proc*  kInfoProc;
+        size_t   mibLength = sizeof(kInfoProc);
 
         managementInfoBase[0] = CTL_KERN;
         managementInfoBase[1] = KERN_PROC;
         managementInfoBase[2] = KERN_PROC_PID;
         managementInfoBase[3] = aPid;
-        managementInfoBase[4] = sizeof(struct kinfo_proc);
+        managementInfoBase[4] = sizeof(kInfoProc);
         managementInfoBase[5] = 1;
 
-        if (::sysctl(managementInfoBase, 6, NULL, &mibLength, NULL, 0) == -1) {
-            kWarning() << "first sysctl() call failed with code" << errno;
+        if (::sysctl(managementInfoBase, 6, &kInfoProc, &mibLength, NULL, 0) == -1) {
+            kWarning() << "sysctl() call failed with code" << errno;
             return false;
         }
 
-        kInfoProc = (struct kinfo_proc*)malloc(mibLength);
-
-        if (::sysctl(managementInfoBase, 6, kInfoProc, &mibLength, NULL, 0) == -1) {
-            kWarning() << "second sysctl() call failed with code" << errno;
-            free(kInfoProc);
-            return false;
-        }
-
-        setName(kInfoProc->p_comm);
-        setPid(kInfoProc->p_pid);
-        setParentPid(kInfoProc->p_ppid);
-        setForegroundPid(kInfoProc->p_tpgid);
-        setUserId(kInfoProc->p_uid);
-        setUserName(kInfoProc->p_login);
-
-        free(kInfoProc);
+        setName(kInfoProc.p_comm);
+        setPid(kInfoProc.p_pid);
+        setParentPid(kInfoProc.p_ppid);
+        setForegroundPid(kInfoProc.p_tpgid);
+        setUserId(kInfoProc.p_uid);
+        setUserName(kInfoProc.p_login);
         return true;
     }
 
@@ -785,7 +776,7 @@ class OpenBSDProcessInfo : public UnixProcessInfo (pub
         managementInfoBase[2] = aPid;
         managementInfoBase[3] = whatMib;
 
-        do {
+        while (true) {
             len *= 2;
             nbuf = realloc(buf, len);
             if (nbuf == NULL) {
@@ -794,8 +785,13 @@ class OpenBSDProcessInfo : public UnixProcessInfo (pub
 
             buf = nbuf;
             rc = ::sysctl(managementInfoBase, 4, buf, &len, NULL, 0);
-            kWarning() << "sysctl() call failed with code" << errno;
-        } while (rc == -1 && errno == ENOMEM);
+            if (rc != -1) {
+                break;
+            } else if (errno != ENOMEM) {
+                kWarning() << "sysctl() call failed with code" << errno;
+                break;
+            }
+        }
 
         if (nbuf == NULL || rc == -1) {
             free(buf);
